package com.cunshe.service;


import com.cunshe.common.FileUtil;
import com.cunshe.dao.ProductDao;
import com.cunshe.dao.VideoDao;
import com.cunshe.domain.Product;
import com.cunshe.domain.Video;
import com.github.pagehelper.PageHelper;
import com.sunkee.commons.utils.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * 视频
 *
 * @author xiao
 */
@Service
public class VideoService {

    @Autowired
    private VideoDao videoDao;
    @Autowired
    private ProductDao productDao;

    /**
     * 新增
     */
    @Transactional(rollbackFor = Exception.class)
    public int insert(Video record) {
        videoDao.insert(record);

        // 修改产品
        updateProduct(record);

        return 1;
    }

    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    public int delete(Long id) {
        Video video = get(id);
        if(video == null) {
            return 0;
        }
        videoDao.delete(id);

        // 修改产品
        updateProduct(video);

        FileUtil.deleteFile(video.getVideoUrl());
        FileUtil.deleteFile(video.getVideoImageUrl());

        return 1;
    }

    /**
     * 修改
     */
    @Transactional(rollbackFor = Exception.class)
    public int update(Video record) {
        Video video = get(record.getId());
        if(video == null){
            return 0;
        }
        int update = videoDao.update(record);

        long p1 = Optional.ofNullable(video.getProductId()).orElse(0L);
        long p2 = Optional.ofNullable(record.getProductId()).orElse(0L);

        if(p1 != 0 && p2 == 0){
            record.setProductId(p1);
        }

        // 修改产品
        updateProduct(record);

        return update;
    }

    private void updateProduct(Video record){
        if (record.getProductId() != null && record.getProductId() != 0) {

            List<Video> byProductId = getByProductId(record.getProductId());
            String videoUrl = "";
            String videoImageUrl = "";
            for (Video video : byProductId) {
                if(StringUtils.isBlank(videoUrl)){
                    videoUrl = video.getVideoUrl();
                } else {
                    videoUrl += "," + video.getVideoUrl();
                }

                if(StringUtils.isBlank(videoImageUrl)){
                    videoImageUrl = video.getVideoImageUrl();
                } else {
                    videoImageUrl += "," + video.getVideoImageUrl();
                }
            }

            Product product = Product.builder()
                    .id(record.getProductId())
                    .videoUrl(videoUrl)
                    .videoImageUrl(videoImageUrl)
                    .build();
            productDao.update(product);
        }
    }

    /**
     * 查询列表
     */
    public Video get(Long id) {
        if (id == null) {
            return null;
        }
        return videoDao.list(Video.builder().id(id).build()).stream()
                .findFirst()
                .orElse(null);
    }

    /**
     * 查询列表
     */
    public List<Video> getByProductId(Long productId) {
        if (productId == null) {
            return null;
        }
        return list(Video.builder().productId(productId).build());
    }

    /**
     * 查询列表
     */
    public List<Video> list(Video record) {
        PageHelper.orderBy(" order_num DESC ");
        return videoDao.list(record);
    }

}